package com.akuma.redmine.service.impl;

import com.akuma.common.util.JsonUtils;
import com.akuma.redmine.entity.Project;
import com.akuma.redmine.entity.ProjectQuery;
import com.akuma.redmine.entity.ProjectView;
import com.akuma.redmine.entity.ProjectsQuery;
import com.akuma.redmine.exception.RedMineException;
import com.akuma.redmine.service.IProjectService;
import com.akuma.redmine.util.RedMineHttpRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by akuma on 2016/10/12.
 *
 * @author akuma
 */
public class ProjectServiceImpl implements IProjectService {

    public ProjectsQuery findAll() throws RedMineException {
        String s = null;
        try {
            s = RedMineHttpRequest.adminGet("projects.json");
        } catch (Exception e) {
            RedMineException.throwException(e);
        }
        return JsonUtils.readValue(s, ProjectsQuery.class);
    }

    public List<ProjectQuery> getTreeProjects() throws RedMineException {
        ProjectsQuery allProjects = findAll();
        if (allProjects == null) {
            return null;
        }
        return allProjects.getTreeProjects();
    }

    public ProjectQuery get(Long id) throws RedMineException {
        Map<String, String> params = new HashMap<String, String>();
        String s = null;
        try {
            s = RedMineHttpRequest.adminGet(params, "projects/{}.json", id);
        } catch (Exception e) {
            RedMineException.throwException(e);
        }
        return JsonUtils.readValue(s, ProjectQuery.class);
    }

    public Integer create(ProjectView project) throws RedMineException {
        String s = null;
        try {
            s = RedMineHttpRequest.adminPost(project, "projects.json");
        } catch (Exception e) {
            RedMineException.throwException(e);
        }
        System.out.println(s);
        return 0;
    }

    public Integer update(ProjectQuery project) throws RedMineException {
        return null;
    }

    public void delete(Long id) throws RedMineException {

    }

}
